<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "300px",
    },
    xData: {
      type: Array,
      default: () => {
        return ["Industries", "Technology", "Forex", "Gold", "Forecasts"];
      },
    },
    yData: {
      type: Array,
      default: () => {
        return [
          { value: 320, name: "Industries" },
          { value: 240, name: "Technology" },
          { value: 149, name: "Forex" },
          { value: 100, name: "Gold" },
          { value: 59, name: "Forecasts" },
        ];
      },
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");

      this.chart.setOption({
        tooltip: {
          trigger: "item",
        },
        textStyle: {
          color: "#fff",
        },
        color:['#02A7F0','#95F204'],//修改图表颜色
        legend: {
          left: "center",
          top: "0",
          data: this.xData,
          textStyle: {
            color: "#fff",
          },
        },
        // graphic: {
        //   type: "text",
        //   left: "center",
        //   top: "center",
        //   style: {
        //     //text:'用户统计\n'+'100', //使用“+”可以使每行文字居中
        //     text: "总量100", //使用“+”可以使每行文字居中
        //     textAlign: "center",
        //     fill: "#fff",
        //     width: 30,
        //     height: 1,
        //   },
        // },
        series: [
          {
            name: "",
            type: "pie",
            // roseType: "radius",  //玫瑰饼图
            radius: [15, 40],
            center: ["50%", "55%"],
            data: this.yData,
            animationEasing: "cubicInOut",
            animationDuration: 2600,
            labelLine: {
              // 设置指示线的长度
              normal: {
                length: 4,
                length2: 1,
              },
            },
            label: {
              show: true,
              position: "inner",
              alignTo: "edge",
              margin: 10,
              textStyle: {
                fontWeight: 300,
                fontSize: 10,
              },
              formatter: "{d}%",
            },
          },
          {
            name: "",
            type: "pie",
            // roseType: "radius",  //玫瑰饼图
            radius: [15, 40],
            center: ["50%", "55%"],
            data: this.yData,
            animationEasing: "cubicInOut",
            animationDuration: 2600,
            labelLine: {
              // 设置指示线的长度
              normal: {
                length: 4,
                length2: 1,
              },
            },
            label: {
              show: true,
              position: "outside",
              alignTo: "edge",
              margin: 10,
              textStyle: {
                fontWeight: 300,
                fontSize: 10,
              },
              formatter: "{b}: {c}",
            },
          },
        ],
      });
    },
  },
};
</script>
